/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiAppMCMK.java
 *
 * Created on Feb 2, 2011, 4:48:55 PM
 */
package br.com.mcdev.app;

import br.com.mcdev.util.ApiConn;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

/**
 *
 * @author tigun
 */
public class GuiAppMCMK extends javax.swing.JFrame {

    private ApiConn aConn = new ApiConn("10.2.1.53", 8728);
    private InfoMK infoMKForm;
    private MCMKLogin mmLogin;

    /** Creates new form GuiAppMCMK */
    public GuiAppMCMK() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new javax.swing.JDesktopPane();
        menuBar = new javax.swing.JMenuBar();
        menu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        contentMenuItem = new javax.swing.JMenuItem();
        aboutMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        menu.setText("Menu");

        openMenuItem.setText("Informations");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        menu.add(openMenuItem);

        saveMenuItem.setText("Save");
        menu.add(saveMenuItem);

        saveAsMenuItem.setText("Save As ...");
        menu.add(saveAsMenuItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        menu.add(exitMenuItem);

        menuBar.add(menu);

        helpMenu.setText("Help");

        contentMenuItem.setText("Contents");
        helpMenu.add(contentMenuItem);

        aboutMenuItem.setText("About");
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
//        mmLogin = new MCMKLogin(this, true);
//        mmLogin.setLocationRelativeTo(this);
//        mmLogin.setLocation(300, 300);
//        mmLogin.setVisible(true);

        aConn.start();
        try {
            aConn.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(MCMKLogin.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (aConn.isConnected()) {

            String confirm = aConn.login("admin", "tigrao123456".toCharArray());
            if (confirm.equals("successful")) {
            } else {
                JOptionPane.showMessageDialog(null, "Password Falhou");

            }

        } else {
            JOptionPane.showMessageDialog(null, "Falha de Conexão, verifique os dados");

        }

    }//GEN-LAST:event_formWindowOpened

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        if (this.infoMKForm == null) {
            this.infoMKForm = new InfoMK(this,aConn);
            this.desktopPane.add(this.infoMKForm);
        }
        this.openWindow(this.infoMKForm);
    }//GEN-LAST:event_openMenuItemActionPerformed

    private void openWindow(JComponent obj) {
        if (!obj.isVisible()) {
            if (!obj.isValid()) {
                desktopPane.add(obj);
            }

            obj.setVisible(true);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GuiAppMCMK().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem contentMenuItem;
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenu menu;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    // End of variables declaration//GEN-END:variables
}
